home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sysdeps / mach / hurd / __kill.c < prev    next >
C/C++ Source or Header  |  1994-04-27  |  3KB  |  96 lines

  1. /* Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <errno.h>
  20. #include <sys/types.h>
  21. #include <signal.h>
  22. #include <hurd.h>
  23. #include <hurd/port.h>
  24. #include <hurd/signal.h>
  25. #include <hurd/msg.h>
  26.  
  27. /* Send signal SIG to process number PID.  If PID is zero,
  28.    send SIG to all processes in the current process's process group.
  29.    If PID is < -1, send SIG to all processes in process group - PID.  */
  30. int
  31. __kill (pid_t pid, int sig)
  32. {
  33.   int delivered = 0;        /* Set when we deliver any signal.  */
  34.   error_t err;
  35.   mach_port_t proc;
  36.   struct hurd_userlink ulink;
  37.  
  38.   inline void kill_pid (pid_t pid) /* Kill one PID.  */
  39.     {
  40.       /* SIGKILL is not delivered as a normal signal.
  41.      Sending SIGKILL to a process means to terminate its task.  */
  42.       if (sig == SIGKILL)
  43.     /* Fetch the process's task port and terminate the task.  We
  44.        loop in case the process execs and changes its task port.
  45.        If the old task port dies after we fetch it but before we
  46.        send the RPC, we get MACH_SEND_INVALID_DEST; if it dies
  47.        after we send the RPC request but before it is serviced, we
  48.        get MIG_SERVER_DIED.  */
  49.     do
  50.       {
  51.         task_t refport;
  52.         err = __proc_pid2task (proc, pid, &refport);
  53.         if (!err)
  54.           {
  55.         err = __task_terminate (refport);
  56.         __mach_port_deallocate (__mach_task_self (), refport);
  57.           }
  58.       } while (err != MACH_SEND_INVALID_DEST &&
  59.            err != MIG_SERVER_DIED);
  60.       else
  61.     err = HURD_MSGPORT_RPC (__proc_getmsgport (proc, pid, &msgport),
  62.                 ({ err = __proc_pid2task (proc, pid,
  63.                               &refport);
  64.                    if (err)
  65.                      err = __proc_getsidport (proc, &refport);
  66.                    err; }),
  67.                 __sig_post (msgport, sig, refport));
  68.       if (! err)
  69.     delivered = 1;
  70.     }
  71.  
  72.   proc = _hurd_port_get (&_hurd_ports[INIT_PORT_PROC], &ulink);
  73.  
  74.   if (pid <= 0)
  75.     {
  76.       /* Send SIG to each process in pgrp (- PID).  */
  77.       mach_msg_type_number_t npids, i;
  78.       pid_t *pids;
  79.       
  80.       err = __proc_getpgrppids (proc, - pid, &pids, &npids);
  81.       if (!err)
  82.     {
  83.       for (i = 0; i < npids; ++i)
  84.         kill_pid (pids[i]);
  85.       __vm_deallocate (__mach_task_self (),
  86.                (vm_address_t) pids, npids * sizeof (pids[0]));
  87.     }
  88.     }
  89.   else
  90.     kill_pid (pid);
  91.  
  92.   _hurd_port_free (&_hurd_ports[INIT_PORT_PROC], &ulink, proc);
  93.  
  94.   return delivered ? 0 : __hurd_fail (err);
  95. }
  96.